
const basiCode = require('../util/baseCode.js');
const dateFormat = require('dateformat');
// 版本
const versionModel = require('../model/versionModel.js');
const CryptoJS = require("crypto-js");

/*
**name:新增用户
* mixnick： mixnick
**
*/

/**
 * 会员
 * @param {nickName: string} 明文nick
 * @param {session: string} 
 */
exports.getMember = async (mix_nick, cloud) => {
    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.crm.member.identity.get',
            autoSession: true,
            data: {
                'mix_nick': mix_nick,
            }
        });
        return basiCode(0, { isOk: 1, msg: '会员请求成功', isMember: result.result.member_info ? true : false, data: result.result.member_info });
    } catch (err) {
        return await basiCode(0, { isOk: 0, msg: err });
    }
};

/**
 * 入会链接
 * @param {url: string} 回调链接
 */
exports.getMemberUrl = async (ctx) => {
    const { data: { url = "" }, cloud } = ctx
    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.crm.member.joinurl.get',
            autoSession: true,
            data: {
                'callback_url': url,
                'extra_info': '{\"source\":\"isvapp\",\"activityId\":\"12345677\",\"entrance\":\"hudong\"}',
            }
        });
        return basiCode(0, { isOk: 1, msg: '入会链接请求成功', data: result });
    } catch (err) {
        return await basiCode(0, { isOk: 0, msg: err });
    }
};


/**
 * 实例化版本
 * @param {url: string} 回调链接
 */
exports.initV = async (ctx) => {
    // console.log(ctx.data);
    const { data: { icon, name, description, template_id, template_version, mainUserOpenId }, cloud } = ctx
    // console.log("用户主id", mainUserOpenId);

    try {
        console.log(icon);
        const result = await cloud.topApi.invoke({
            api: 'taobao.miniapp.template.instantiate',
            autoSession: true,
            data: {
                'clients': 'taobao',
                'description': description,
                'ext_json': '{ \"name\":\"test\"}',
                'icon': icon,
                'name': name,
                'template_id': template_id,
                'template_version': template_version
            }
        });


            
            //将数据写入数据库
            const resultz = {
                "appkey": result.data.appkey,
                "app_id":  result.data.app_id,
                "app_name": result.data.app_name,
                "app_description":result.data.app_description ,
                "app_icon": result.data.app_icon,
                "pre_view_url": result.data.pre_view_url,
                "app_version": result.data.app_version
            };
            // return await basiCode(0, { isOk: 1, msg: '请求成功', data: result });
            return await versionModel.addVersion(ctx,resultz);

      

    } catch (err) {
        //模拟处理mock


        return { isOk: 1, msg: err };

    }
};




/**
 * 查询
 */
exports.getVersion = async (ctx, mainUserOpenId) => {

    const { data, cloud } = ctx
    try {
        return await versionModel.getVersion(ctx, mainUserOpenId);

    } catch (err) {

        return await basiCode(0, { isOk: 0, msg: err });

    }
};



/**
 * 获取线上版本号
 */
exports.getonline_Version = async (ctx, mainUserOpenId) => {

    const { data, cloud } = ctx
    try {
        return await versionModel.getonline_Version(ctx, mainUserOpenId);

    } catch (err) {

        return await basiCode(0, { isOk: 0, msg: err });

    }
};


/**
 * 更新版本号
 */
exports.update_Version = async (ctx) => {

    const { cloud, mixNick, userNick, main_openid, mainUserOpenId, data: { app_id, template_id, template_version } } = ctx

    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.miniapp.template.updateapp',
            autoSession: true,
            data: {
                'clients': 'taobao',
                'app_id': app_id,
                'template_id': template_id,
                'template_version': template_version
            }
        });
        console.log("我是result",result);

        return await versionModel.update_Version(ctx, mainUserOpenId);

        // return await basiCode(0, { isOk: 1, msg: '请求成功', data: result });

    } catch (err) {
        //模拟处理mock
        
        return  { isOk: 0, msg: err };

    }
};


/**
 * 上传版本
 */
exports.Upload_Version = async (ctx) => {

    const { cloud, mixNick, userNick, main_openid, mainUserOpenId, data: { app_id, template_id, template_version, app_version } } = ctx

    try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.miniapp.template.onlineapp',
            autoSession: true,
            data: {
                'clients': 'taobao,tmall',
                'app_id': app_id,
                'template_id': template_id,
                'template_version': template_version,
                'app_version': app_version
            }
        });

        return  { isOk: 0, msg: '请求成功', data: result };

    } catch (err) {

        return { isOk: 1, msg: err };

    }
};